import { MessageBox, Message } from "element-ui";

// 这里封装全局公用方法
const util = {
  // 批量删除统一提示
  delInfo(message = "删除") {
    return MessageBox.confirm(`您确定要${message}吗?`, `${message}提示`, {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    });
  },
  // 深拷贝
  copy(obj) {
    return JSON.parse(JSON.stringify(obj));
  },
  //防止重复点击按钮,这里不太好使，待研究
  preventReclick(fn, context, delay = 1000) {
    let timer = null;
    return function () {
      let arg = arguments;
      clearTimeout(timer);
      timer = setTimeout(() => {
        fn.apply(context, arg);
      }, delay);
    };
  },
  // 判断对象类型
  getObjType(obj) {
    var map = {
      "[object Boolean]": "boolean",
      "[object Number]": "number",
      "[object String]": "string",
      "[object Function]": "function",
      "[object Array]": "array",
      "[object Date]": "date",
      "[object RegExp]": "regExp",
      "[object Undefined]": "undefined",
      "[object Null]": "null",
      "[object Object]": "object",
    };
    // if (obj instanceof Element) {
    //   return 'element';
    // }
    return map[Object.prototype.toString.call(obj)];
  },
  // 判断按钮权限
  judgeButtonShow(obj) {
    // 解决js文件缓存问题
    if (persArr == null) {
      persArr = JSON.parse(sessionStorage.getItem("permissions"));
    }
    for (let i in obj) {
      obj[i].forEach((it) => {
        if (persArr.find((j) => j == it.prop)) {
          it.alwaysShow = true;
        }
      });
    }
    return this.copy(obj);
  },
  // form表单统一验证
  validateId(rule, value, callback) {
    if (value !== "") {
      var reg = /(^\d{15}$)|(^\d{16}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (reg.test(value) == false) {
        callback(new Error("身份证输入不合法"));
      } else {
        callback();
      }
    } else {
      callback();
    }
  },
  makeData(data) {
    let arr = [];
    for (let i in data) {
      arr.push({
        label: data[i],
        id: i,
      });
    }
    return arr;
  },
  dateAllTransformation(item, type) {
    const date = item ? new Date(item) : new Date();
    var year = date.getFullYear(); //年
    var month =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1; //月
    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //日
    var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //时
    var minutes =
      date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //分
    var seconds =
      date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //秒
    var milliseconds =
      date.getMilliseconds() < 10
        ? "0" + date.getMilliseconds()
        : date.getMilliseconds(); //毫秒
    if (type == 1) {
      return (
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hour +
        ":" +
        minutes +
        ":" +
        seconds +
        "." +
        milliseconds
      );
    } else if (type == 2) {
      return (
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hour +
        ":" +
        minutes +
        ":" +
        seconds
      );
    } else if (type == 3) {
      // 年月日
      return year + "-" + month + "-" + day;
    } else {
      // return (
      //   year +
      //   "-" +
      //   month +
      //   "-" +
      //   day +
      //   " " +
      //   hour +
      //   ":" +
      //   minutes +
      //   ":" +
      //   seconds
      // );
      return new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).replace("T", " ")
    }
  },
  filterObjAttribute(obj, type) {
    if (obj[type]) {
      obj.startTime = obj[type][0];
      obj.endTime = obj[type][1];
    } else {
      obj.startTime = "";
      obj.endTime = "";
    }
    let finalObj = {}
    for (let i in obj) {
      if (i != type) {
        finalObj[i] = obj[i]
      }
    }
    return finalObj
  },
  // 阿拉伯数字和汉字对换
  capitalizeNum(num) {
    let n = String(num);
    if (!/(^[1-9]\d*$)/) {
      return "非法数字";
    }
    var uppercase = "千百亿千百十万千百十个";
    var nLength = n.length;
    var newStr = "";
    if (uppercase.length - nLength < 0) {
      return "数字过长";
    }
    uppercase = uppercase.substr(uppercase.length - nLength);
    for (var i = 0; i < nLength; i++) {
      newStr += "零一二三四五六七八九".charAt(n[i]) + uppercase.charAt(i);
    }
    newStr = newStr.substr(0, newStr.length - 1);
    // 因为12 变成一十二(正常为12)，所以单独处理11-19
    if (nLength == 2 && newStr[0] == "一") {
      newStr = newStr.substr(1, newStr.length - 1);
    }
    // 处理10，20，30，40，... 90等带零的情况
    // 因为这里只有100以内，所以暂时只去掉零（粗暴）--TODO待优化
    if (newStr.includes("零")) {
      newStr = newStr.substr(0, newStr.length - 1);
    }
    return newStr;
  },
  checkImg(file) {
    let fileType = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
    // , 'svg'
    if (!["jpeg", 'jpg', 'png'].includes(fileType)) {
      Message.error({
        message: '上传的图片格式不正确'
      })
      return false
    } else {
      return true
    }

  }
};
export default util